// Sample plugin. Use CTRL+T to toggle the toolbar
nhn.husky.SE_ToolbarToggler = $Class({
	name : "SE_ToolbarToggler",

	$init : function(oAppContainer){
		this._assignHTMLObjects(oAppContainer);
	},

	_assignHTMLObjects : function(oAppContainer){
		oAppContainer = $(oAppContainer) || document;
		this.toolbarArea = cssquery.getSingle(".tool", oAppContainer);
	},
	
	$ON_MSG_APP_READY : function(){
		this.oApp.exec("SE_TOGGLE_TOOLBAR", []);
		this.oApp.exec("REGISTER_HOTKEY", ["ctrl+shift+alt+t", "SE_TOGGLE_TOOLBAR", []]);
	},
	
	$ON_SE_TOGGLE_TOOLBAR : function(){
		this.toolbarArea.style.display = (this.toolbarArea.style.display == "none")?"block":"none";
		this.oApp.exec("MSG_EDITING_AREA_SIZE_CHANGED", []);
	}
});

//파일업로드 클레스 추가
nhn.husky.SE_FileUpload = $Class({
	name : "SE_FileUpload",
	
	$init : function(elAppContainer){
		this._assignHTMLObjects(elAppContainer);
	},
	
	_assignHTMLObjects : function(elAppContainer){
		this.oFileUploadLayer = cssquery.getSingle("DIV.husky_seditor_fileupload_layer", elAppContainer);
	},
	
	$ON_MSG_APP_READY : function(){
		this.oApp.exec("REGISTER_UI_EVENT", ["fileupload", "click", "SE_TOGGLE_FILEUPLOAD_LAYER"]);
	},
	
	$ON_SE_TOGGLE_FILEUPLOAD_LAYER : function(){
		this.oApp.exec("TOGGLE_TOOLBAR_ACTIVE_LAYER", [this.oFileUploadLayer]);
	}
});

nhn.husky.SE_PhotoEditor = $Class({
	name : "SE_PhotoEditor",
	
	$init : function(elAppContainer){
		this._assignHTMLObjects(elAppContainer);
	},
	
	_assignHTMLObjects : function(elAppContainer){
		this.oFileUploadLayer = cssquery.getSingle("DIV.husky_seditor_photoeditor_layer", elAppContainer);
	},
	
	$ON_MSG_APP_READY : function(){
		this.oApp.exec("REGISTER_UI_EVENT", ["photoeditor", "click", "SE_OPEN_PHOTOEDITOR"]);
	},
	
	$ON_SE_OPEN_PHOTOEDITOR : function(){
		window.open("http://s.lab.naver.com/pe/service?exportTo="+escape("http://"+document.domain+"/_Editor/photosave.asp") + "&exportField=file&exportTitle=Save To Editor", "", "width=1024,height=768,top=0,left=0");
	}
});

//파일업로드 클레스 추가
nhn.husky.SE_MultiMedia = $Class({
	name : "SE_MultiMedia",
	
	$init : function(elAppContainer){
		this._assignHTMLObjects(elAppContainer);
	},
	
	_assignHTMLObjects : function(elAppContainer){
		var tmp = null;
		this.oMultiMediaLayer = cssquery.getSingle("DIV.husky_seditor_multimedia_layer", elAppContainer);
		this.oTBLURL          = cssquery.getSingle(".multimedia_tblurl", elAppContainer);
		this.oTBLSRC          = cssquery.getSingle(".multimedia_tblsrc", elAppContainer);
		tmp = cssquery("INPUT", this.oMultiMediaLayer);
		
		this.oVodTypeAVI= tmp[0];
		this.oVodTypeSRC= tmp[1];
		
		this.oVodUrl    = tmp[2];
		this.oVodWidth  = tmp[3];
		this.oVodHeight = tmp[4];
		
		tmp = cssquery("TEXTAREA", this.oMultiMediaLayer);
		this.oVodSource = tmp[0];
		
		tmp = cssquery("BUTTON", this.oMultiMediaLayer);
		this.oButton_Insert = tmp[0];
		this.oButton_Cancel = tmp[1];
		
	},
	
	$ON_MSG_APP_READY : function(){
		this.oApp.exec("REGISTER_UI_EVENT", ["multimedia", "click", "SM_TOGGLE_MULTIMEDIA_LAYER"]);
		this.oApp.registerBrowserEvent(this.oButton_Insert, "click", "SM_INSERT_MULTIMEIDA");
		this.oApp.registerBrowserEvent(this.oButton_Cancel, "click", "SM_CLOSE");
		this.oApp.registerBrowserEvent(this.oVodTypeAVI, "click", "SM_TOGGLE_FORM");
		this.oApp.registerBrowserEvent(this.oVodTypeFLV, "click", "SM_TOGGLE_FORM");
		this.oApp.registerBrowserEvent(this.oVodTypeSRC, "click", "SM_TOGGLE_FORM");
	},
	
	$ON_SM_TOGGLE_FORM: function(){
		if(this.oVodTypeSRC.checked){
			this.oTBLURL.style.display="none";
			this.oTBLSRC.style.display="block";
		} else {
			this.oTBLURL.style.display="block";
			this.oTBLSRC.style.display="none";
		}
	},
	
	$ON_SM_TOGGLE_MULTIMEDIA_LAYER : function(){
		this.oApp.exec("TOGGLE_TOOLBAR_ACTIVE_LAYER", [this.oMultiMediaLayer]);
	},

	$ON_SM_INSERT_MULTIMEIDA : function(){
		var sMedia = this._getMediaString();
		if(sMedia != null) {
			this.oApp.exec("PASTE_HTML", [sMedia]);
			this.oApp.exec("ST_CLOSE", []);
		}
	},

	$ON_SM_CLOSE : function(){
		this.oApp.exec("HIDE_ACTIVE_LAYER", []);
	},
	
	_validateURL : function(sURL){
		return /^(http|https|ftp|mailto):(?:\/\/)?((\w|-)+(?:[\.:@](\w|-))+)(?:\/|@)?([^"\?]*?)(?:\?([^\?"]*?))?$/.test(sURL);
	},

	// need to do something about the table width as the same HTML code is being used to the actual table and the preview table
	_getMediaString : function(){
		var sMedia = null;
		var iHeight= this.oVodHeight.value == "" ? 500 : parseInt(this.oVodHeight.value); 
		var iWidth = this.oVodWidth.value == "" ? 408 : parseInt(this.oVodWidth.value);
		var sUrl   = this.oVodUrl.value;
		var sSrc   = this.oVodSource.value;
		if(this.oVodTypeAVI.checked) {
			if(sUrl!=""){
				if(this._validateURL(sUrl)){
					if(/(\.swf|\.flv)$/.test(sUrl)){
						sMedia = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='" + iWidth + "' height='" + iHeight + "'><param name='movie' value='"+ sUrl+"' /><param name='wmode' value='transparent' /><param name='allowScriptAccess' value='always' /><param name='allowFullScreen' value='true' /><embed src='"+ sUrl+"' wmode='transparent' width='" + iWidth + "' height='" + iHeight + "' allowScriptAccess='always' allowFullScreen='true' type='application/x-shockwave-flash' /></object>";
					} else {
						sMedia = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='" + iWidth + "' height='" + iHeight + "'><param name='movie' value='"+ sUrl+"' /><param name='wmode' value='transparent' /><param name='allowScriptAccess' value='always' /><param name='allowFullScreen' value='true' /><embed src='"+ sUrl+"' wmode='transparent' width='" + iWidth + "' height='" + iHeight + "' allowScriptAccess='always' allowFullScreen='true' type='application/x-shockwave-flash' /></object>";
					}
				} else {
					alert("URL이 올바르지 않습니다");
					this.oVodUrl.select();
				}
			} else {
				alert("URL을 입력하세요");
				this.oVodUrl.focus();
			}
		/*
		} else if(this.oVodTypeFLV.checked) {
			if(sUrl!=""){
				if(this._validateURL(sUrl)){
					sMedia = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='" + iWidth + "' height='" + iHeight + "'><param name='movie' value='"+ sUrl+"' /><param name='wmode' value='transparent' /><param name='allowScriptAccess' value='always' /><param name='allowFullScreen' value='true' /><embed src='"+ sUrl+"' wmode='transparent' width='" + iWidth + "' height='" + iHeight + "' allowScriptAccess='always' allowFullScreen='true' type='application/x-shockwave-flash' /></object>";
				} else {
					alert("URL이 올바르지 않습니다");
					this.oVodUrl.select();
				}
			} else {
				alert("URL을 입력하세요");
				this.oVodUrl.focus();
			}
		*/
		} else if(this.oVodTypeSRC.checked) {
			if(sSrc == ""){
				alert("소스를 입력하세요");
				this.oVodSource.focus();
			} else {
				sMedia = sSrc;
			}
		}
		return sMedia;
	}

});

function SE_RegisterCustomPlugins(oEditor, elAppContainer){
	oEditor.registerPlugin(new nhn.husky.SE_FileUpload(elAppContainer));
	oEditor.registerPlugin(new nhn.husky.SE_PhotoEditor(elAppContainer));
	oEditor.registerPlugin(new nhn.husky.SE_MultiMedia(elAppContainer));
}
